const TableService = require('../service/table');
const retCode = require('../utils/retcode');


class TableController {

  /**
   * 分页查询
   * @param  {object} ctx   上下文
   * @return {object}       结果
   */

  async TableDataFilter(ctx) {
    let form = ctx.request.body;
    // console.log(form)
    const args = {
      current: parseInt(form.current),
      pageSize: parseInt(form.pageSize),
      years: form.years,
      deviceType: form.deviceType
    };
    console.log(args)

    //用户信息List
    let ListResult = await TableService.getListByFilter(args,
      // limit: ctx.pagination.limit,
      // offset: ctx.pagination.offset
    );

    let NumResult = await TableService.getCountByFilter(args);

    let ResultData = {
      ListResult: ListResult,
      current: args.current,
      total: NumResult[0]['count(1)'],
      pageSize: args.pageSize
    }
    ctx.body = {
      status: 200,
      statusText: 'ok',
      data: ResultData
    };
    console.log(ResultData.pageSize)
    // console.log(ResultData.ListResult)
    console.log(ResultData.total)


    // This is needed in order to expose the length in `Content-Range` header.
    // ctx.pagination.length = userNumResult[0].total

  }

  async mockdata(ctx) {
    ctx.body = {
      success: true,
      message: "T1108泰福高正品不锈钢真空保温壶保温瓶水壶暖",
      data: {
        foo: "DJ美国百丽耐热"
      }
    };
  }

  async getPie(ctx) {
    let form = ctx.request.body;
    console.log(form)
    // const args = {
    //   id: parseInt(form.id),
    // };
    // console.log(args)
    let Result = await TableService.getPie({})
    console.log(Result)

    if (Result.affectedRows != 0) {
      ctx.body = {
        status: 200,
        statusText: 'ok',
        data: Result
      };

    } else {
      ctx.body = {
        status: 400,
        statusText: 'fail',
        data: {}
      };
    }

  }

  async delData(ctx) {
    let form = ctx.request.body;
    // console.log(form)
    const args = {
      id: parseInt(form.id),
    };
    // console.log(args)
    let Result = await TableService.delData(args)
    // console.log(Result)
    if (Result.affectedRows != 0) {
      ctx.body = {
        status: 200,
        statusText: 'ok',
        // data: ResultData
      };

    } else {
      ctx.body = {
        status: 400,
        statusText: 'fail',
        data: {}
      };
    }

  }

  async updateData(ctx) {
    let form = ctx.request.body;
    console.log(form)
    const args = {
      id: parseInt(form.id),
      value: form.values,
      state: form.state
    };


    let Result = await TableService.updateData(args)

    if (Result.affectedRows != 0) {
      //   const argss = {
      //     current: parseInt(args.state.current),
      //     pageSize: parseInt(args.state.pageSize),
      //     years: args.state.years,
      //     deviceType: args.state.deviceType
      //   };
      //
      //   let ListResult = await TableService.getListByFilter(argss);
      //
      //   let NumResult = await TableService.getCountByFilter(argss);
      //
      //   let ResultData = {ListResult: ListResult, current: args.state.current, total: NumResult[0]['count(1)'], pageSize: args.state.pageSize}
      ctx.body = {
        status: 200,
        statusText: 'ok',
        // data: ResultData
      };

    } else {
      ctx.body = {
        status: 400,
        statusText: 'fail',
        data: {}
      };
    }

  }

  async getDeviceTypeName(ctx) {
    let args = {}
    let DeviceResult = await TableService.getDeviceTypeName(args);
    console.log(DeviceResult)
    let arr = [];
    try {
      DeviceResult.forEach(function (e, i) {
        arr.push(e.DeviceTypeName)
      });
      console.log(arr)
      ctx.body = {
        status: 200,
        statusText: 'ok',
        data: arr
      };
    } catch (e) {
      ctx.body = {
        status: 400,
        statusText: 'fail',
        data: arr
      };
    }

  }

  async yearFilter(ctx) {
    let form = ctx.request.body
    let pageIndex = parseInt(form.page)
    let limit = parseInt(form.pageSize)
    let offset = (pageIndex - 1) * limit
    const args = {
      ymonth: form.ymonth,
      devicetype: form.devicetype,
      limit: limit,
      offset: offset
    }

    //List
    let data = await userService.getYearListByFilter(args)

    let totalResult = await userService.getYearCountByFilter(args)
    let total = totalResult[0].total
    ctx.body = {
      status: retCode.Success,
      statusText: '查询成功',
      data: {
        table: data,
        pagination: {
          pageIndex: pageIndex,
          recordTotal: total,
          pageSize: limit,
        },
      },
    }

  }
}


module.exports = new TableController();
